<template>
  <div class="login-container">
    <!-- 背景装饰元素 -->
    <div class="bg-particles"></div>
    <div class="bg-glow"></div>

    <div class="login-wrapper">
      <!-- 登录卡片 -->
      <div class="login-card">
        <!-- 左侧品牌展示区 -->
        <div class="login-brand">
          <div class="brand-content">
            <div class="brand-logo">
              <svg width="60" height="60" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                <path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M2 17L12 22L22 17" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M2 12L12 17L22 12" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
              </svg>
            </div>
            <h1 class="brand-title">AdminPro-plus-max</h1>
            <p class="brand-desc">智能数据采集与分析可视化平台</p>
          </div>
        </div>

        <!-- 右侧登录表单区 -->
        <div class="login-form-container">
          <el-form ref="formRef" :rules="data.rules" :model="data.form" class="login-form">
            <div class="form-header">
              <h2 class="form-title">账户登录</h2>
              <p class="form-subtitle">请输入您的账号信息</p>
            </div>

            <el-form-item prop="username" class="form-item">
              <div class="input-group">
                <el-icon class="input-icon"><User /></el-icon>
                <el-input
                    v-model="data.form.username"
                    placeholder="请输入账号"
                    class="custom-input"
                    :class="{ 'input-focused': data.usernameFocus }"
                    @focus="data.usernameFocus = true"
                    @blur="data.usernameFocus = false"
                ></el-input>
              </div>
            </el-form-item>
            <el-form-item prop="password" class="form-item">
              <div class="input-group">
                <el-icon class="input-icon"><Lock /></el-icon>
                <el-input
                    show-password
                    v-model="data.form.password"
                    placeholder="请输入密码"
                    class="custom-input"
                    :class="{ 'input-focused': data.passwordFocus }"
                    @focus="data.passwordFocus = true"
                    @blur="data.passwordFocus = false"
                ></el-input>
              </div>
            </el-form-item>

            <div class="form-actions">
              <el-button
                  @click="login"
                  class="login-btn"
                  :loading="data.loading"
              >
                <template v-if="!data.loading">登录</template>
                <template v-if="data.loading">
                  <el-icon><Loading /></el-icon>
                  <span>登录中...</span>
                </template>
              </el-button>
            </div>

            <div class="form-footer">
              <span>还没有账号？</span>
              <a href="/register" class="register-link">立即注册</a>
            </div>
          </el-form>
        </div>
      </div>

      <!-- 页脚 -->
      <div class="login-footer">
        <p>© 2025 青山. 保留所有权利</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { User, Lock, UserFilled, Loading } from "@element-plus/icons-vue";
import request from "@/utils/request.js";
import { ElMessage } from "element-plus";
import { useRouter } from 'vue-router'; // 导入 useRouter
import {  useMainStore } from '@/store/index.js';
import {login as loginApi} from "@/api/login.js";

const router = useRouter();
const mainStore = useMainStore();


const formRef = ref();

const data = reactive({
  loading: false,
  usernameFocus: false,
  passwordFocus: false,
  roleFocus: false,
  form: {
    username: "",
    password: "",
  },
  rules: {
    username: [
      { required: true, message: "请输入账号", trigger: "blur" }
    ],
    password: [
      { required: true, message: "请输入密码", trigger: "blur" }
    ]
  }
});

const login = () => {
  formRef.value.validate( async (valid) => {
    if (valid) {
      data.loading = true;
      try{
          await mainStore.login(data.form)
          ElMessage.success('登录成功');
          setTimeout(router.push("/Manager/home"),100);
      }catch (error){
        ElMessage.error(error.msg || '登录失败，请检查账号密码');
      }finally {
        //  无论成功/失败，都关闭加载状态
        data.loading = false
      }
    }
  })
};

//formRef.value.validate( async (valid) => //这玩意是配合elementplus来使用的，只要是表单验证看是否符合逻辑
// request.post('/login', data.form).then(res => {
//   data.loading = false;
//   if (res.code === '200') {
//     console.log(res);
//     // 储存后台返回的数据
//     const token=res.data.token;
//     localStorage.setItem('token', token);
//     localStorage.setItem('xm-pro', JSON.stringify(res.data));
//     ElMessage.success('登录成功');
//     setTimeout(() => {
//       router.push("/Manager/home");
//     }, 100);
//   } else {
//     ElMessage.error(res.msg || '登录失败，请检查账号密码');
//   }
// }).catch(() => {
//   data.loading = false;
//   ElMessage.error('网络错误，请稍后重试');
// });
// 创建粒子背景效果
const createParticles = () => {
  const particlesContainer = document.querySelector('.bg-particles');
  if (!particlesContainer) return;

  const particleCount = 50;

  for (let i = 0; i < particleCount; i++) {
    const particle = document.createElement('div');
    particle.classList.add('particle');

    // 随机样式
    const size = Math.random() * 5 + 1;
    particle.style.width = `${size}px`;
    particle.style.height = `${size}px`;
    particle.style.left = `${Math.random() * 100}%`;
    particle.style.top = `${Math.random() * 100}%`;
    particle.style.opacity = Math.random() * 0.5 + 0.2;
    particle.style.animationDuration = `${Math.random() * 20 + 10}s`;
    particle.style.animationDelay = `${Math.random() * 5}s`;

    particlesContainer.appendChild(particle);
  }
};

// 页面加载完成后创建粒子效果
setTimeout(createParticles, 100);
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: relative;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
}

/* 背景粒子效果 */
.bg-particles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

.particle {
  position: absolute;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  animation: float linear infinite;
}

@keyframes float {
  0% {
    transform: translateY(0) translateX(0);
  }
  25% {
    transform: translateY(-20px) translateX(10px);
  }
  50% {
    transform: translateY(-40px) translateX(0);
  }
  75% {
    transform: translateY(-20px) translateX(-10px);
  }
  100% {
    transform: translateY(0) translateX(0);
  }
}

/* 背景光晕效果 */
.bg-glow {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 800px;
  height: 800px;
  background: radial-gradient(circle, rgba(76, 175, 80, 0.1) 0%, rgba(0, 0, 0, 0) 70%);
  z-index: 0;
  filter: blur(50px);
}

.login-wrapper {
  position: relative;
  z-index: 1;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

/* 登录卡片样式 */
/* 增强输入框聚焦动画 */
.custom-input, .custom-select {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
  background-position: 0 0;
  background-size: 200% 100%;
  background-image: linear-gradient(to right, transparent 50%, rgba(255,255,255,0.05) 50%);
}

.custom-input:focus, .custom-select:focus {
  background-position: 100% 0;
  border-color: #4cc9f0 !important;
}

/* 登录按钮增强效果 */
.login-btn {
  position: relative;
  overflow: hidden;
  z-index: 1;
}

.login-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.1), transparent);
  transition: all 0.6s ease;
  z-index: -1;
}

.login-btn:hover::before {
  left: 100%;
}
.login-card {
  width: 100%;
  max-width: 900px;
  height: 550px;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  box-shadow: 0 10px 50px rgba(0, 0, 0, 0.3);
  display: flex;
  overflow: hidden;
  transition: all 0.3s ease;
}

.login-card:hover {
  box-shadow: 0 15px 60px rgba(0, 0, 0, 0.4);
  transform: translateY(-5px);
}

/* 品牌区域样式 */
.login-brand {
  flex: 1;
  background: linear-gradient(135deg, #4361ee 0%, #3a0ca3 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.login-brand::before {
  content: '';
  position: absolute;
  width: 200px;
  height: 200px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  top: -50px;
  right: -50px;
}

.login-brand::after {
  content: '';
  position: absolute;
  width: 150px;
  height: 150px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  bottom: -30px;
  left: -30px;
}

.brand-content {
  text-align: center;
  color: white;
  z-index: 1;
  padding: 40px;
}

.brand-logo {
  margin-bottom: 30px;
  animation: pulse 3s infinite;
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

.brand-title {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 15px;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}

.brand-desc {
  font-size: 16px;
  opacity: 0.9;
  line-height: 1.6;
}

/* 表单区域样式 */
.login-form-container {
  flex: 1;
  padding: 60px 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-form {
  width: 100%;
  max-width: 350px;
  margin: 0 auto;
}

.form-header {
  margin-bottom: 40px;
  text-align: center;
}

.form-title {
  font-size: 26px;
  font-weight: 600;
  color: #ffffff;
  margin-bottom: 8px;
}

.form-subtitle {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.6);
}

.form-item {
  margin-bottom: 22px;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.input-icon {
  position: absolute;
  left: 15px;
  color: rgba(255, 255, 255, 0.5);
  transition: all 0.3s ease;
}

.custom-input, .custom-select {
  width: 100%;
  padding: 15px 15px 15px 45px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.05);
  color: white;
  font-size: 14px;
  transition: all 0.3s ease;
}

.custom-input::placeholder {
  color: rgba(255, 255, 255, 0.4);
}

.custom-input:focus, .custom-select:focus {
  border-color: #4361ee;
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 0 0 3px rgba(67, 97, 238, 0.2);
}

.input-focused .input-icon {
  color: #4361ee;
}

.form-actions {
  margin-top: 30px;
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
  padding: 14px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(90deg, #4361ee 0%, #3a0ca3 100%);
  border: none;
  color: white;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.login-btn:hover {
  background: linear-gradient(90deg, #3a0ca3 0%, #4361ee 100%);
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(67, 97, 238, 0.3);
}

.login-btn:active {
  transform: translateY(0);
}

.form-footer {
  text-align: center;
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
}

.register-link {
  color: #4cc9f0;
  margin-left: 5px;
  text-decoration: none;
  transition: all 0.2s ease;
}

.register-link:hover {
  color: #4895ef;
  text-decoration: underline;
}

/* 页脚样式 */
.login-footer {
  margin-top: 40px;
  color: rgba(255, 255, 255, 0.4);
  font-size: 12px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-card {
    flex-direction: column;
    height: auto;
    max-width: 400px;
  }

  .login-brand {
    padding: 40px 20px;
  }

  .login-form-container {
    padding: 40px 20px;
  }

  .brand-title {
    font-size: 26px;
  }

  .form-title {
    font-size: 22px;
  }
}
</style>
